<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class km_perakitan extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		// Uses Model
		$this->load->model('km_perakitan_model', 'Master');
		$this->load->model('km_detail_perakitan_model', 'Detail');
		$this->load->model('kst_combo_model', 'Combo');
		$this->load->model('kst_store_procedure_model', 'SP');
		// Secure Page With Session
		$this->secure_session();
	}
	
	function index(){
		//Define Combo
		$data['arr_unit']	= $this->Combo->unit_combo();
		//$data['arr_application']	= $this->Combo->ITApplication();
		// Display Index
		$data['title'] = $this->session_menu_label;
		
		viewByTemplate('km_perakitan/index', $data, 'grid');
	}
	
	function getdata()
	{
		// Ajax Response For Datagrid
		echo $this->Master->GetJSON();
	}
	
	function dtl_sample2_data()
	{
		// Ajax Response For Datagrid
		echo $this->Detail->GetJSON(post('km_perakitan'));
	}
	
	function create()
	{
		// Display Form Create
		$data['title'] = "New Order";
		$data['data'] = NULL;
		$data['arr_unit']	= $this->Combo->unit_combo();
		$data['arr_reference']	= $this->Combo->SampleReference();
		$data['mode'] = "create";
		viewByTemplate('km_perakitan/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Data Master";
		$data['data'] = $this->Master->GetSingleView(get('kode_perakitan'));
		
		// Define Combo Data
		$data['mode'] = "edit";
		viewByTemplate('km_perakitan/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Master";
		$data['data'] = $this->Master->GetSingleView(get('kode_perakitan'));
		
		$data['mode'] = "view";
		viewByTemplate('km_perakitan/form', $data, 'form');
	}
	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty		= $this->MessageTemplate("ER001");
		$tpl_duplicate	= $this->MessageTemplate("ER002");

		// Validation
		if (post("kode_perakitan") == '') {
			$error->Add("kode_perakitan", "Kode Perakitan", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT COUNT(1) FROM km_perakitan_head WHERE UPPER(kode_perakitan) = '".@strtoupper(@trim(post("kode_perakitan")))."'")) > 0) {
			$error->Add("kode_perakitan", "Kode Perakitan", $tpl_duplicate);
		}
		
		return $error;
		
	}
	
	function ajax_post()
	{
		// Ajax Response For Submit Form
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";
		
		// Get Error Template
		$tpl_insert_success = $this->MessageTemplate("CS001");
		$tpl_update_success = $this->MessageTemplate("CS002");
		
		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data Array
				$data	= post_by_key(array('kode_perakitan','unit','tgl','nama_barang_head'));
				
				// Set Primary Key Variable
				$data['kode_perakitan']	= @strtoupper(@trim(post("kode_perakitan")));
				$kode_perakitan	= $data['kode_perakitan'];
				
				//$data['jenis_pembayaran']	= @strtoupper(@trim(post("jenis_pembayaran")));
				
				// Set Date Format
				$data['tgl']	= ($data['tgl'] == '' ? NULL : date_format3($data['tgl'], 'YYYY-MM-DD'));				
				if (post("mode") == "create")
				{
					$ls_genid	= $kode_perakitan;
					//$this->SP->SP_PEMBELIAN($data['no_po'],$data['kd_vendor'],$data['tgl_order'],$data['jenis_pembayaran']);
					$this->Master->Add($data);
					//$ls_response = $tpl_insert_success['message_id'];
					$ls_response = "Data Tersimpan";
				}
				else if (post("mode") == "edit")
				{
					unset($data['kode_perakitan']);
					
					$this->Master->Update(	$data, 
											array('kode_perakitan'	=> $kode_perakitan));
					
					//$ls_response = $tpl_update_success['message_id'];
					$ls_response = "Data Tersimpan [edited]";
				}
								
				// ----- >> Detail Sample 2
				$dtl_sample2	= $this->data['dtl_sample2'];
				$total_deleted	= $dtl_sample2['num_row']['deleted'];
				$total_inserted	= $dtl_sample2['num_row']['inserted'];
				$total_updated	= $dtl_sample2['num_row']['updated'];
				
				for($i=0,$max_i=$total_deleted; $i<$max_i; $i++){
					$row = $dtl_sample2['deleted'][$i];					
					$this->Detail->DeleteSingle($row['kode_perakitan']);
				}
				
				for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
					$row = $dtl_sample2['inserted'][$i];					
					$row['qty_order'] = (float)$row['qty_order'];
					$row['kd_barang'] = $row['kd_barang'];
					$row['kode_perakitan'] = $kode_perakitan;					
					$this->Detail->Add($row);
				}
				
				for($i=0,$max_i=$total_updated; $i<$max_i; $i++){
					$row = $dtl_sample2['updated'][$i];					
					$urutan	= $row['urutan'];
					unset($row['urutan']);					
					$row['qty_order'] = (float)$row['qty_order'];					
					$this->Detail->Update($row, array(	'kode_perakitan'	=> $kode_perakitan, 
														'urutan'	=> $urutan));
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}
	
		
	function ajax_delete()
	{
		$ls_result	= "";
		$kode_perakitan	= get('kode_perakitan');
		
		if ($kode_perakitan != "")
		{
			// Delete Single
			$arr_rule	= array('kode_perakitan' => $kode_perakitan);
			$ls_result = $this->Master->DeleteSingle($arr_rule);
		}
		else
		{
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('kode_perakitan' => $row['kode_perakitan']);
				$ls_result .= $this->Master->DeleteSingle($arr_rule);
			}
						
		}
		
		echo $ls_result;
	}
	
}
?>